import 'package:demo1223/model/twolist.dart';
import 'package:demo1223/service/service_method.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:provide/provide.dart';
import '../../provide/classification_list.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../routers/application.dart';

class TwoGoodlist extends StatefulWidget {
  //  goodlist = Provide.value<Goodslist>(context).goodlist.data.goodData;
  TwoGoodlist({Key key, this.id1, this.id2, this.goodlist}) : super(key: key);
  String id1 = "";
  String id2 = "";
  int page = 2;
  List<GoodData> goodlist = [];
  @override
  _TwoGoodlistState createState() => _TwoGoodlistState();
}

class _TwoGoodlistState extends State<TwoGoodlist> {
  @override
  List<GoodData> goodlistS = [];
  List<GoodData> newgoodlist = [];
  Widget build(BuildContext context) {
    return Provide<Goodslist>(builder: (context, child, val) {
      if (widget.goodlist != null) {
        return Container(
            color: Colors.white,
            child: EasyRefresh(
                footer: ClassicalFooter(
                  bgColor: Colors.red,
                  textColor: Colors.white,
                  noMoreText: '',
                  infoText: '加载中',
                  loadedText: '上拉加载更多',
                ),
                child: ListView.builder(
                  itemCount: widget.goodlist.length,
                  physics: BouncingScrollPhysics(),
                  itemBuilder: (context, index) {
                    return Container(
                      width: ScreenUtil().setWidth(750),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          border: Border.all(width: 1, color: Colors.black45)),
                      child: GestureDetector(
                        behavior: HitTestBehavior.opaque, //点击区域
                        child: Row(
                          children: <Widget>[
                            _goodsimg(widget.goodlist[index].coverImg),
                            _goodscontent(
                                widget.goodlist[index].goodsName,
                                widget.goodlist[index].goodsModel,
                                widget.goodlist[index].goodsNo)
                          ],
                        ),
                        onTap: () {
                          Application.router.navigateTo(context,
                              "detail?id=${widget.goodlist[index].goodsId}");
                          print("你点击了商品ID${widget.goodlist[index].goodsId}");
                        },
                      ),
                    );
                  },
                ),
                onLoad: () async {
                  await Future.delayed(
                    Duration(seconds: 1),
                    () async {
                      print(widget.id1);
                      print(widget.id2);
                      await Provide.value<Goodslist>(context)
                          .getgoodlist(widget.id1, widget.id2, widget.page);
                      //     print(widget.page);
                      var datas =
                          Provide.value<Goodslist>(context).goodlitsData;
                      setState(() {
                        widget.page++;
                        newgoodlist = [];
                        widget.goodlist += datas;
                      });
                    },
                  );
                }));
      } else {}
    });
  }

  Widget _goodsimg(url) {
    return Image.network(
      url,
      width: ScreenUtil().setWidth(218),
      height: ScreenUtil().setHeight(231),
    );
  }

  Widget _goodscontent(name, model, no) {
    return Container(
      alignment: Alignment.centerLeft,
      width: ScreenUtil().setWidth(470),
      child: Column(
        children: <Widget>[
          _goodsname(name),
          _goodmodel(model),
          _goodsno(no),
        ],
      ),
    );
  }

  Widget _goodsname(name) {
    return Container(
        child: Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            Expanded(
              child: Text(name),
            )
          ],
        )
      ],
    ));
  }

  Widget _goodmodel(model) {
    return Row(
      children: <Widget>[Text('型号:' + model)],
    );
  }

  Widget _goodsno(no) {
    return Row(
      children: <Widget>[Text("订货号:" + no)],
    );
  }
}

// class Homepage extends StatefulWidget {
//   @override
//   _TwoGoodslist createState() => TwoGoodslist();
// }

// class TwoGoodslist extends StatelessWidget {
//   int page = 1;
//   List<GoodData> goodlist = [];

//   @override
//   Widget build(BuildContext context) {
//     return Provide<Goodslist>(
//       builder: (context, child, val) {
//         var goodlist = Provide.value<Goodslist>(context).goodlist.data.goodData;
//         print(goodlist);
//         if (goodlist != null) {
//           return Container(
//               color: Colors.white,
//               child: EasyRefresh(
//                   footer: ClassicalFooter(
//                     bgColor: Colors.red,
//                     textColor: Colors.white,
//                     noMoreText: '',
//                     infoText: '加载中',
//                     loadedText: '上拉加载更多',
//                   ),
//                   child: ListView.builder(
//                     itemCount: goodlist.length,
//                     physics: BouncingScrollPhysics(),
//                     itemBuilder: (context, index) {
//                       return Container(
//                         width: ScreenUtil().setWidth(750),
//                         decoration: BoxDecoration(
//                             color: Colors.white,
//                             border:
//                                 Border.all(width: 1, color: Colors.black45)),
//                         child: GestureDetector(
//                           behavior: HitTestBehavior.opaque, //点击区域
//                           child: Row(
//                             children: <Widget>[
//                               _goodsimg(goodlist[index].coverImg),
//                               _goodscontent(
//                                   goodlist[index].goodsName,
//                                   goodlist[index].goodsModel,
//                                   goodlist[index].goodsNo)
//                             ],
//                           ),
//                           onTap: () {
//                             Application.router.navigateTo(context,
//                                 "detail?id=${goodlist[index].goodsId}");
//                             print("你点击了商品ID${goodlist[index].goodsId}");
//                           },
//                         ),
//                       );
//                     },
//                   ),
//                   onLoad: () async {
//                     await Future.delayed(Duration(seconds: 1), () {

//                     });
//                   })
//               //       onLoad: () async{
//               //         await Future.delayed(Duration(seconds: 1),(){
//               //         Provide.value<Goodslist>(context).getgoodlist(id1, id2);
//               //         // List<GoodData> newGoodsList = Provide.value<Goodslist>(context).goodlist.data.goodData;
//               //         // print(newGoodsList);
//               // //         var formData = {
//               // //                 "category_id1":id1,
//               // // "category_id2":id2,
//               // // "page":page,
//               // //         };
//               // //         request('getCateGoodData',formData: formData).then((val){
//               // //           var data = val['data']['goodData'];
//               // //           List<Map> newGoodsList = (data as List).cast();
//               // //           print(newGoodsList);
//               // //         });
//               //         // print(list);
//               //         // goodlist = newGoodsList;
//               //         // print(goodlist);
//               //         });

//               //       },

//               );
//         } else {
//           return Text('正在加载中');
//         }
//       },
//     );
//   }

//   Widget _goodsimg(url) {
//     return Image.network(
//       url,
//       width: ScreenUtil().setWidth(218),
//       height: ScreenUtil().setHeight(231),

//     );
//   }

//   Widget _goodscontent(name, model, no) {
//     return Container(
//       alignment: Alignment.centerLeft,
//       width: ScreenUtil().setWidth(470),
//       child: Column(
//         children: <Widget>[
//           _goodsname(name),
//           _goodmodel(model),
//           _goodsno(no),
//         ],
//       ),
//     );
//   }

//   Widget _goodsname(name) {
//     return Container(
//         child: Column(
//       children: <Widget>[
//         Row(
//           children: <Widget>[
//             Expanded(
//               child: Text(name),
//             )
//           ],
//         )
//       ],
//     ));
//   }

//   Widget _goodmodel(model) {
//     return Row(
//       children: <Widget>[Text('型号:' + model)],
//     );
//   }

//   Widget _goodsno(no) {
//     return Row(
//       children: <Widget>[Text("订货号:" + no)],
//     );
//   }
// }
